package com.fsds.crem.web.interceptors;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.fsds.crem.web.entity.Users;

public class AuthInterceptor implements HandlerInterceptor {

	private static final Logger logger = Logger.getLogger(AuthInterceptor.class);

	private List<String> excludeUrls;

	public List<String> getExcludeUrls() {

		return excludeUrls;
	}

	public void setExcludeUrls(List<String> excludeUrls) {

		this.excludeUrls = excludeUrls;
	}

	/*
	 * 在controller后拦截
	 * 
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	public void afterCompletion(HttpServletRequest request,
															HttpServletResponse response,
															Object object,
															Exception exception) throws Exception {

	}

	/*
	 * 
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	public void postHandle(	HttpServletRequest request,
													HttpServletResponse response,
													Object object,
													ModelAndView modelAndView) throws Exception {

	}

	/*
	 * 在controller前拦截
	 * 
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {

		String requestPath = getRequestPath2(request);// 用户访问的资源地址
		HttpSession session = request.getSession();

		logger.debug(session.getId());
		logger.info("requestPath:"+requestPath);
		
		if (excludeUrls.contains(requestPath)) {

			// 如果该请求不在拦截范围内，直接返回true
			return true;
		}
		else {
			
			Users users = (Users) session.getAttribute("user");
			
			// 用户已登录
			if(users != null){
				
				return true;
			}
			// 用户未登录，session失效
			else{
				
				// 跳转到登录页面
				request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
				
				return false;
			}
		}
	}

	/**
	 * 截取访问路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestPath(HttpServletRequest request) {

		String requestPath = request.getRequestURI() + "?" + request.getQueryString();

		// 去掉其他参数
		if (requestPath.indexOf("&") > -1) {

			requestPath = requestPath.substring(0, requestPath.indexOf("&"));
		}

		// 去掉项目路径
		requestPath = requestPath.substring(request.getContextPath().length() + 1);
		return requestPath;
	}
	
	/**
	 * 截取访问路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestPath2(HttpServletRequest request) {

		String requestPath = request.getRequestURI();

		// 去掉其他参数
		if (requestPath.indexOf("&") > -1) {

			requestPath = requestPath.substring(0, requestPath.indexOf("&"));
		}
		// 去掉jsessionid参数
		if(requestPath.indexOf(";") > -1){
			
			requestPath = requestPath.substring(0, requestPath.indexOf(";"));
		}

		// 去掉项目路径
		requestPath = requestPath.substring(request.getContextPath().length() + 1);
		return requestPath;
	}
}
